<script lang="ts" setup>
import { get, post, message, loadFile } from '@/utils'
import { FormInstance } from '@arco-design/web-vue'
import useModal from '@/hooks/useModal'
import AuditModal from '@/components/Modal/Audit.vue'
import useTable from '@/hooks/useTable'
const props = defineProps<{
  show: boolean
  id: number | string
}>()
const emit = defineEmits(['update:show', 'reset', 'confirm'])
const { store } = useModal()
const { formatColumns } = useTable()
const searchQuery = reactive({
  targetId: props.id,
})
const formRef = ref()
const actionRef = ref()

const pagination = {
  showPageSize: true,
  pageSize: 20,
}

const request = (params: any, sort: any, filters: any): Promise<any> => {
  return new Promise((resolve, reject) => {
    const query = { ...params, size: params.pageSize }
    Reflect.deleteProperty(query, 'pageSize')
    get(
      {
        url: '/server-social/admin/comment/page',
        data: query,
      },
      ({ data, code }) => {
        data.records.forEach(item => {
          item.isLeaf = false
        })
        resolve({
          data: data.records,
          total: data.total,
          success: code == 200,
        })
      }
    )
  })
}

const columns = formatColumns([
  {
    title: '用户头像',
    dataIndex: 'fromUserAvatar',
    width: 80,
    valueType: 'avatar',
  },
  {
    title: '用户昵称',
    dataIndex: 'fromUserName',
    width: 80,
  },
  {
    title: '评论内容',
    dataIndex: 'content',
    width: 200,
    ellipsis: true,
  },
  {
    title: '点赞量',
    dataIndex: 'likeNum',
    width: 80,
  },
  {
    title: '是否显示',
    dataIndex: 'isShow',
    width: 80,
  },

  {
    title: '是否置顶',
    dataIndex: 'isTop',
    width: 80,
  },
  {
    title: '审核备注',
    dataIndex: 'remark',
    width: 80,
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 90,
    valueEnum: {
      '-1': { text: '审核拒绝', status: 'Error' },
      0: { text: '待审核', status: 'Processing' },
      1: { text: '审核通过', status: 'Success' },
    },
  },
  {
    title: '操作',
    dataIndex: 'options',
    width: 120,
    fixed: 'right',
    btnList: [
      {
        name: '审核',
        disabled: ({ record }) => record.status != 0,
        handle: ({ record }) => store.handleShow(record),
      },
      {
        name: '删除',
        type: 'popconfirm',
        tip: '确认删除该评论吗?',

        handle: ({ record }) => {
          post({ url: '/server-social/admin/comment/remove?ids=' + record.id }, ({ code, msg }) => {
            if (code == 200) actionRef.value.reload()
            message({
              type: code == 200 ? 'success' : 'error',
              content: msg,
            })
          })
        },
      },
    ],
  },
])
const loadMore = (record, done) => {
  get({ url: '/server-social/admin/comment/reply/page', data: { parentId: record.id, current: 1, size: 999 } }, ({ data }) => {
    data.records.forEach(record => {
      record.isLeaf = true
    })
    done(data.records)
  })
}
const changeShow = (id, isShow) => {
  post({ url: '/server-social/admin/comment/show', data: { id, isShow } }, ({ code, msg }) => {
    if (code == 200) {
      actionRef.value.reload()
    }
    message({
      type: code == 200 ? 'success' : 'error',
      content: msg,
    })
  })
}
</script>
<template>
  <CModal v-model:visible="props.show" :footer="false" width="1100px" title="评论管理" @cancel="emit('update:show', false)">
    <ProTable :columns="columns" :request="request" :loadMore="loadMore" row-key="id" :form-ref="ref => (formRef = ref)" :pagination="pagination" header-title="" column-resizable :scroll="{ x: 1000 }" :params="searchQuery" :bordered="{ cell: true }" :action-ref="ref => (actionRef = ref)">
      <template #option-render="{ dom, reset }">
        <!-- <component :is="dom[1]" />
        <component :is="dom[0]" /> -->
      </template>
      <template #isShow="{ record }">
        <a-switch v-model="record.isShow" :checked-value="1" :unchecked-value="0" :before-change="val => changeShow(record.id, val)">
          <template #checked> 显示 </template>
          <template #unchecked> 隐藏 </template>
        </a-switch>
      </template>
    </ProTable>
  </CModal>
  <AuditModal v-if="store.show" v-model:show="store.show" idKey="id" :id="store.id" @confirm="actionRef.reload" api="/server-social/admin/comment/audit" />
</template>
<style lang="less" scoped></style>
